﻿package Chapter02;
// 將 StepIntoJava.java 內容替換為下列代碼即可運行
//======================================================================
//public class StepIntoJava {
//	public static void main(String[] args) {
//		GuessGame app = new GuessGame();
//		app.run();
//	}	
//}
//======================================================================

// 猜數字遊戲，課本範例，物件導向程式設計
public class GuessGame {
	Player p1;
	Player p2;
	Player p3;
	
	public void run() {
		p1 = new Player();
		p2 = new Player();
		p3 = new Player();
		
		Player[] players = {p1, p2, p3};

		int[] playerGuess = {0, 0, 0};
		boolean[] playerIsRight = {false, false, false};
		
		int targetNumber = (int)(Math.random() * 10);
		System.out.println("I'm thinking of a number between 0 and 9...");
		boolean hasWinner = false;

		while (true) {
			System.out.println("Number to guess is " + targetNumber);

			for (int i = 0; i < players.length; i++) {
				players[i].guessByRandom();
				
				playerGuess[i] = players[i].intNumber;
				System.out.println("Player"+ (i+1) +" guessed " + playerGuess[i]);
				
				if (playerGuess[i] == targetNumber) {
					playerIsRight[i] = true;
					hasWinner = true;
				}
			}
			
			if (hasWinner) {
				System.out.println("We have a winner!");
				for (int i = 0; i < players.length; i++) {
					System.out.println("Player " + (i+1) + " got it right? " + playerIsRight[i]);
				}
				System.out.println("Game is over.");
				break;
			} else {
				System.out.println("Players will have to try again.");
			}
		}
	} // end method
} // end class
